import 'dart:ui';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:study_demo/base/BaseController.dart';

import '../../generated/assets.dart';

class ShaderController extends BaseController {
  FragmentShader? shader1;
  FragmentShader? shader2;
  FragmentShader? shader3;
  ui.Image? uiImage;

  @override
  void onInit() {
    super.onInit();
    _loadShader();
    _loadImage();
  }

  _loadShader() async {
    String path1 = 'shaders/color1.frag';
    String path2 = 'shaders/color2.frag';
    String path3 = 'shaders/color3.frag';
    FragmentProgram program1 = await FragmentProgram.fromAsset(path1);
    shader1 = program1.fragmentShader();
    FragmentProgram program2 = await FragmentProgram.fromAsset(path2);
    shader2 = program2.fragmentShader();
    FragmentProgram program3 = await FragmentProgram.fromAsset(path3);
    shader3 = program3.fragmentShader();
    update();
  }

  _loadImage() {
    final AssetImage assetImage = AssetImage(Assets.imagesWbhh);
    final ImageStream stream = assetImage.resolve(ImageConfiguration.empty);

    stream.addListener(ImageStreamListener((ImageInfo info, bool _) {
      uiImage = info.image;
      update(); // Notify listeners that the image is ready
    }));
  }
}
